package com.cqwshzj.aware;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/7 18:59
 */

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/7
 * @since 1.0.0
 */
@Component
public class AppContextHolder implements ApplicationContextAware {

	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	//-------------------------------------------

	public static ApplicationContext getContext() {
		return context;
	}

	//--------------------------------------------------------------------

	public static Object getBean(String name) throws BeansException {
		if (context != null) {
			return context.getBean( name );
		}
		return null;
	}

	public static <T> T getBean(String name, Class<T> aClass) throws BeansException {
		if (context != null) {
			return context.getBean( name , aClass );
		}
		return null;
	}

	public static Object getBean(String name, Object... args) throws BeansException {
		if (context != null) {
			return context.getBean( name , args);
		}
		return null;
	}

	public static <T> T getBean(Class<T> aClass) throws BeansException {
		if (context != null) {
			return context.getBean( aClass );
		}
		return null;
	}

	public static <T> T getBean(Class<T> aClass, Object... args) throws BeansException {
		if (context != null) {
			return context.getBean( aClass, args );
		}
		return null;
	}

	//------------

	public static boolean containsBean( String name ){
		if (context != null) {
			return context.containsBean( name );
		}
		return false;
	}

	public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.isSingleton( name );
		}
		return true;
	}

	public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.isPrototype( name );
		}
		return false;
	}

	public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.isTypeMatch( name, typeToMatch );
		}
		return false;
	}

	public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.isTypeMatch( name, typeToMatch );
		}
		return false;
	}

	public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.getType( name );
		}
		return null;
	}


	/*public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
		if (context != null) {
			return context.getType( name, allowFactoryBeanInit);
		}
		return null;
	}*/


	public String[] getAliases(String name) {
		if (context != null) {
			return context.getAliases(name);
		}
		return null;
	}

	//------------- biz method


}
